﻿using System;
using BitTorrent.Agents;
using Microsoft.Ccr.Core;

namespace BitTorrent.Client {
    public class ClientAgent : AgentBase {
        private Dispatcher _dispatcher;

        public ClientAgent(Dispatcher dispatcher) : base(new DispatcherQueue("Client", dispatcher)) {
            _dispatcher = dispatcher;

            Port = new Port<AddTorrent>();

            Activate(
                Arbiter.Interleave(
                    // ---------------------------------------------------------------------------------------
                    new TeardownReceiverGroup(Cleanup(s => {
                                                          // TODO (fen): Implement Teardown for ClientAgent
                                                      })),
                    // ---------------------------------------------------------------------------------------
                    new ExclusiveReceiverGroup(),
                    // ---------------------------------------------------------------------------------------
                    new ConcurrentReceiverGroup(Arbiter.Receive(true, Port, AddTorrentHandler))
                    ));
        }

        private void AddTorrentHandler(AddTorrent message) {
            var x = new TorrentAgent(message.TorrentMetaFile, _dispatcher);
        }

        public Port<AddTorrent> Port { get; private set; }
    }

    public class AddTorrent {
        public File.MetaFile TorrentMetaFile { get; set; }
    }
}
